﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using VIT.Library.Web;
using System.Data;
using System.Collections.Generic;

namespace VIT.Web.Admin
{
    public partial class TemplateConfig : VITComponent
    {
        VIT.Provider.TemplateConfig obj;

        protected void Page_Load(object sender, EventArgs e)
        {            
            obj = new VIT.Provider.TemplateConfig();
            if (Request["act"] == "Del")
            {
                Delete(); btnSave.Visible = false;
            }
            else
            {
                try
                {
                    obj.Id = Convert.ToInt32(Request["sId"]);
                }
                catch
                {
                    obj.Id = -1;
                }
                if (obj.Id > 0) btnAdd.Visible = true; else btnAdd.Visible = false;
            }    
            if (!IsPostBack) LoadData();
        }

        #region Methods
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable GetData(string SortColumn)
        {
            return new VIT.Provider.TemplateConfig().GetAll(SortColumn);
        }

        private void LoadData()
        {
            obj.LanguageId = Session["language"].ToString();
            obj.GetSingle();
            LoadLanguages(obj.LanguageId);
            LoadTemplateList(obj.TemplateName);
            LoadConfigType(obj.TemplateConfigBy);
            txtFrom.Text = obj.FromDate.ToString();
            txtTo.Text = obj.ToDate.ToString();
        }

        private void LoadLanguages(string lang)
        {
            Language language = new Language();
            
            ddlLang.DataSource = language.getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = lang;
        }

        private void LoadTemplateList(string template)
        {
            string TemplatePath = URLBaseDir + "Templates";
            ddlTemplate.DataSource = MainCore.ShowDirectories(TemplatePath);
            ddlTemplate.DataBind();
            ddlTemplate.SelectedValue = template;
        }

        private void LoadConfigType(string type)
        {
            ddlConfigBy.DataSource = VIT.DataHelper.DataSource.DataSource.TemplateConfigTypes;
            ddlConfigBy.DataTextField = "Name";
            ddlConfigBy.DataValueField = "Id";
            ddlConfigBy.DataBind();
            ddlConfigBy.SelectedValue = type;
        }

        private bool save()
        {
            obj.LanguageId = ddlLang.SelectedValue;
            obj.TemplateName = ddlTemplate.SelectedValue;
            obj.TemplateConfigBy = ddlConfigBy.SelectedValue;
            obj.FromDate = Convert.ToInt32(txtFrom.Text);
            obj.ToDate = Convert.ToInt32(txtTo.Text);

            return obj.Save();
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblDelete.Visible = false;

            control.Visible = true;
        }

        private void Delete()
        {
            int id;
            try
            {
                id = Convert.ToInt32(Request["sId"]);
            }
            catch
            {
                id = -1;
            }

            if (id > 0)
                if (obj.Delete(id)) setTrueVisible(lblDelete);
                else setTrueVisible(lblThatBai);
        }
        #endregion

        
        #region Events
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            RedirectComponent("Config", "TemplateConfig");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (save()) setTrueVisible(lblThanhCong);
            else setTrueVisible(lblThatBai);
            ListView.DataBind();
        }
        #endregion
    }
}
